<?php
class WebsitesAction extends CommonAction {
	private $pageUrl = '/websites/';
	private $cateObject;
	private $cateInfo = array();
	
	public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Websites');
		$this->modelDataObject = D('Webdata');
		$this->modelViewObject = D('WebsitesView');
		$this->cateObject = D('WebsitesCate');
		$this->webapplyObject = D('Webapply');
		$this->getNav(array($this->webConfig['host_url'].'websites/'));
    }
	
    public function index(){
		$map = array();

		//解析url
		$pUrl = I('p', '');
		$thisPage = 1;
		
		//id3p3;
		$title = '';
		if((substr($pUrl, 0, 1) == 'x' || strpos($pUrl, 'x'))){
			$provinceId = intval(substr($pUrl, strpos($pUrl, 'x')+1));
			$map['province_id'] = $provinceId;
			$this->assign('x', 'x'.$provinceId);	
			
			$this->assign('city', R('Region/getCity',array('provinceId'=>$provinceId)));
			$title .= R('Region/getRegionName', array('id'=>$provinceId));
		}
		if((substr($pUrl, 0, 1) == 'y' || strpos($pUrl, 'y'))){
			$cityId = intval(substr($pUrl, strpos($pUrl, 'y')+1));
			$map['city_id'] = $cityId;
			$this->assign('y', 'y'.$cityId);	
			
			$title .= R('Region/getRegionName', array('id'=>$cityId));
		}
		if((substr($pUrl, 0, 1) == 'c' || strpos($pUrl, 'c'))){
			$cateId = intval(substr($pUrl, strpos($pUrl, 'c')+1));
			$this->assign('c', 'c'.$cateId);	
			$this->cateInfo = $this->cateObject->getInfo($cateId);
			$cateIds =  $this->cateObject->where(array('cate_path'=>array('LIKE', $this->cateInfo['cate_path'].'%')))->getField('group_concat(id)');
			$map['cate_id'] = array('IN', $cateIds);
			
			//获得识别主分类和副分类的key
			$cateActiveKey = explode(',', $this->cateInfo['cate_path']);
			$this->assign('fc', $cateActiveKey[0]); //cate path 中 第0个为主分类	
			$this->assign('sc', $cateActiveKey[1]);	//cate path 中 第1个为副分类
				
			$title .= $this->cateObject->getCateName($cateActiveKey[0]);
			$title .= $this->cateObject->getCateName($cateActiveKey[1]);
		}
		if((substr($pUrl, 0, 1) == 'p' || strpos($pUrl, 'p'))){
			$thisPage = intval(substr($pUrl, strpos($pUrl, 'p')+1));
		}
		
		
		//初始值
		$map['status'] = 1;
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage, $this->webConfig['websites_page_size'], 'is_top DESC,id DESC');
		foreach($list as $key=>$val){
			$list[$key]['url'] = setViewUrl('site', $val);	
		}
		$this->assign('list', $list);
		$this->assign('pageUrl', $this->pageUrl);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage, $this->webConfig['websites_page_size']);
		$this->assign('page',$page);
		//p($page);

		//福建厦门教育学习艺术类的网站分类目录
		$this->setSeo(array_merge($this->cateInfo, array('title'=>$title.'类的网站分类目录')), 'websites', $thisPage);
		
		//分类处理
		$cateTree = $this->cateObject->getCateTree(array(), array($this->cateInfo['id']));
		//获得下级分类
		if(isset($cateActiveKey[0]) && $cateActiveKey[0] > 0){
			foreach($cateTree as $key=>$val){
				if($val['id'] == $cateActiveKey[0]){
					$this->assign('cateSonTree', $val['tree']);
					break;	
				}
			}
		}
		$this->assign('cateTree', $cateTree);
		
		$this->assign('province', R('Region/getProvince'));
		
		$this->display();
    }
	
	
	public function show(){
		$domain = $pUrl = I('domain', '');
		$page = 1;
		
		if($domain){
			$domain = str_replace('_', '.', $domain);
			if(!($info = $this->modelViewObject->getInfo(array('domain'=>$domain)))){
				$this->go404();	
			}
			
			//上一篇，下一篇
			if($info['prev'] = $this->modelObject->getInfo(array('id'=>array('LT', $info['id'])), 'id DESC')){
				$info['prev']['url'] = setViewUrl('site', $info['prev']);	
			}
			if($info['next'] = $this->modelObject->getInfo(array('id'=>array('GT', $info['id'])), 'id ASC')){
				$info['next']['url'] = setViewUrl('site', $info['next']);	
			}
			
			$info['nowPage'] = $page;
			$info['score_avg'] = ceil($info['score'] / $info['score_man']);
			
			$this->assign('info', $info);

			$this->setSeo($info);
			$this->modelObject->where(array('id'=>$info['id']))->setInc('views');
			$this->display();	
		}else{
			$this->go404();
		}
	}
	
	public function addurl(){
		if(IS_POST){
			$cateId     = I('cate_id', 0, 'intval');
			$provinceId = I('province_id', 0, 'intval');
			$cityId     = I('city_id', 0, 'intval');
			$domain 	= I('domain', '', 'strval');
			
			$id         = I('id', 0, 'intval');
			
			if (empty($domain)) {
				$this->error('请输入网站域名!');
			} else {
				if (!isDomain($domain)) {
					$this->error('请输入正确的网站域名!');
				}
			}
			
			if($id > 0 && ($webapplyInfo = $this->webapplyObject->where(array('id'=>$id, 'member_id'=>$this->memberInfo['id']))->find())){//如果id > 0 但是网站信息找不到，那么当成普通的提交
				if($webapplyInfo['is_recycle'] == 1){
					$this->error('该网站已被管理员删除!');		
				}
				
				if($domain != $webapplyInfo['domain']){
					//查询域名是否存在
					if($this->modelObject->getInfo(array('domain'=>$domain)) || $this->webapplyObject->where(array('domain'=>$domain))->find()){
						$this->error('该网站已经存在,不能重复添加!');		
					}
				}
				
				$_POST['status'] = 1;
			}else{
				//查询域名是否存在
				if($this->modelObject->getInfo(array('domain'=>$domain)) || $this->webapplyObject->where(array('domain'=>$domain))->find()){
					$this->error('该网站已经存在,不能重复添加!');		
				}
			}
			if($cateId == 0){
				$this->error('请选择分类!');	
			}
			if($provinceId == 0 || $cityId == 0){
				$this->error('请选择网站所属地区!');	
			}
			
			$_POST['add_time']  = NOW_TIME;
			$_POST['member_id'] = $this->memberInfo['id'];
			if($this->webapplyObject->create()){
				if($id > 0){
					if($insertId = $this->webapplyObject->save()){
						$this->success('编辑成功,请等待管理员审核!', U('/addurl/'));
					}else{
						$this->error('编辑失败,请输入合法的字符!'.$this->webapplyObject->getError());
					}
				}else{
					if($insertId = $this->webapplyObject->add()){
						$this->success('提交成功,请等待管理员审核!', U('/addurl/'));
					}else{
						$this->error('网站提交失败!'.$this->webapplyObject->getError());
					}				}
			}else{
				$this->error('创建数据失败,请联系管理员!'.$this->webapplyObject->getError());
			}
		}else{
			if($this->memberInfo['id'] > 0){
				$id = I('id', 0, 'intval');
				if($id > 0){
					$info = $this->webapplyObject->getInfo(array('id'=>$id, 'member_id'=>$this->memberInfo['id']));
					$this->assign('info', $info);
					$this->assign('city', R('Region/getCity', array('provinceId'=>$info['province_id'])));
				}
			}
			
			$this->setSeo(array(), 'addurl');
			$this->assign('province', R('Region/getProvince'));
			$this->assign('cateOption', R('WebsitesCate/getCateOption'));
			$this->display();	
		}
    }
	
	public function getMeta(){
		$domain = I('domain', '', 'trim');
		
		if (empty($domain)) {
			$this->ajaxReturn(0, '请输入网站域名!', 0);
		} else {
			if (!isDomain($domain)) {
				$this->ajaxReturn(0, '请输入正确的网站域名!', 0);
			}
		}
		
		//判断url 是否已经存在
		if($this->modelObject->where(array('domain'=>$domain))->find() || $this->webapplyObject->where(array('domain'=>$domain))->find()){
			$this->ajaxReturn(0, '域名已经提交过了!', 0);	
		}
		
		$data = array();
		$data = getUrlContent('http://'.$domain);
		
		$this->ajaxReturn($data, '返回数据', 1);
	}
	
	public function getData(){
		$type = I('type', '');
		$webId = I('id', 0);
		
		if (in_array($type, array('ip', 'brank', 'grank', 'srank', 'arank'))) {
			if (!($info = $this->modelDataObject->getInfo(array('web_id'=>$webId)))){
				exit();
			}
			
			//每个月更新一次
			if ((NOW_TIME - (3600 * 24 * 30)) > $info['update_time']) {
				$data = array();
				$data['update_time'] = NOW_TIME;
				//modelDataObject
				#server ip
				if ($type == 'ip') {
					$ip = get_server_ip($info['domain']);
					$data['ip'] = sprintf("%u", ip2long($ip));
				}
				
				#baidu rank
				if ($type == 'brank') {
					$data['brank'] = get_baidu_rank($info['domain']);
				}
			
				#google pagerank
				if ($type == 'grank') {
					 $data['grank'] = get_google_rank($info['domain']);
				}
				
				#sogou rank
				if ($type == 'srank') {
					$data['srank'] = get_sogou_rank($info['domain']);
				}
				
				#alexa
				if ($type == 'arank') {
					$data['arank'] = get_alexa_rank($info['domain']);
				}
				
				$this->modelDataObject->where(array('web_id'=>$webId))->save($data);
			}
		}
	}
	public function outstat(){
		$id    = I('id', 0, 'intval');
		if($id){
			if ($this->modelObject->where(array('id'=>$id))->setInc('outstat')) {
				$this->ajaxReturn(1, '设置成功!', 1);
			}
		}else{
			$this->ajaxReturn(0, '参数错误!', 0);
		}
	}
	
	public function score(){
		$score = I('s', 5, 'intval');
		$id    = I('id', 0, 'intval');
		
		if($id && $score > 0 && $score < 6){
			if (!cookie('score_'.$id)) {
				$this->modelObject->where(array('id'=>$id))->setField(array('score'=>array('exp','score+'.$score), 'score_man'=>array('exp','score_man+1')));
				cookie('score_'.$id, $id, NOW_TIME + 3600);
				$this->ajaxReturn(1, '设置成功!', 1);
			} else {
				$this->ajaxReturn(0, '你已经评分过了!', 0);
			}
		}else{
			$this->ajaxReturn(0, '参数错误!', 0);
		}
	}
}